<?php

include_once 'phpmailer/class.phpmailer.php';

class EmailSender
{
	var $template_group;	//group for the template
	var $template_name;		
	var $body_text;			//Use body text if template not returned. 
	
	var $email_subject;		//Should be set if body_text used, otherwise set from template.
	
	var $field_replacements = array(); // the tags and values to replace in the email e.g. {first_name} => "Joe Blogs", ...
	
	var $sender_address;
	var $sender_name;
	
	var $recipient_address;
	var $recipient_name;
	
	var $cc_addresses = array();	//An array of addresses to cc. 
	
	var $errors = array(); // submission errors
	
	var $mail_host = 'localhost';
	
	function EmailSender($outgoing_address, $sentfrom, $mail_host='')
	{
		$this->sender_address = $outgoing_address;
		$this->sender_name = $sentfrom;
		
		if (!empty($mail_host))
		{
			$this->mail_host = $mail_host;
		}
	}
	
	function addRecipient($address, $name)
	{
		$this->recipient_address = $address;
		$this->recipient_name = $name;
	}
	
	function get_body_text()
	{
		global $DB;
		
		$result = $this->body_text; // If there's no template name/group specified, or the template is not being returned, just return the body text. 		
		
		if((!empty($this->template_group))||(!empty($this->template_name))) // if a custom template was defined, use this
		{
			$sql = "SELECT * FROM exp_templates, exp_template_groups WHERE  ".
				   "template_name = '$this->template_name' AND group_name = '$this->template_group' AND ".
				   "exp_templates.group_id = exp_template_groups.group_id";
//			print($sql."<br/>\n");//TMP
				   
			$query = $DB->query($sql);
			if($query->num_rows > 0)
			{
				$row = $query->result[0];
				$this->email_subject = $row['data_title'];
				$template_text = $row['template_data'];
				
				if (!empty($template_text)) 
				{
					$result = $template_text;
				}
			}
		}
		
		if (sizeof($this->field_replacements) > 0)
		{
			foreach ($this->field_replacements as $key => $value)
			{
				$result = str_replace("{".$key."}", $value, $result);
			}
		}
					
		return $result;		
	}
	
	function sendMail()
	{
		//Set up the phpmailer object and send it. 
		$mail = new PHPMailer();
		$mail->LE = $this->get_os_line_ending();
		$mail->CharSet  = 'utf-8';
		$mail->Host     = $_SERVER['HTTP_HOST'];
//		$mail->Host		= self::get_mail_host();
//		$mail->HostName	= self::get_mail_host();
		$mail->Host		= $this->mail_host;
		$mail->HostName	= $this->mail_host;
		$mail->From     = $this->sender_address;
		$mail->FromName = $this->sender_name;
		$mail->Mailer   = "smtp";	//- I think this is the default setting in the pmpmailer class anyway. 
		$mail->Subject = $this->email_subject;
		
		$body = $this->get_body_text();
		$mail->IsHTML(true);
		$mail->Body = $body;
		$mail->Body = preg_replace("/(\r\n|\n)/s", "<br/>".$mail->LE, $body);
		
		$mail->AltBody = $body;
		
		$mail->AddAddress($this->recipient_address, $this->recipient_name);
		
		
		if (sizeof($this->cc_addresses) > 0) // add any additional CCs
		{
			foreach ($this->$cc_addresses as $cc_email)
			{
//					print("Adding CC address: $cc_email<br/>");//TMP
				$mail->AddCC($cc_email, $cc_email);
			}
		}
		
		$success = $mail->Send();
		if(!$success)
		{
			$this->errors[] = $mail->Error_Info;
		}
		return $success;
	}

	/**
	 *@return the name of the mail server host (depending on which server we are on)
	 */
/*	 
	static function get_mail_host()
	{
		//$host = 'ncbwebdev2';
		$host = 'ncbpostal';
		
//		if ($_SERVER['HTTP_HOST'] == 'intranet') // running in the live location
//		{
//			$host = 'localhost';
//		}
		
//		print("host: $host<br/>\n");//TMP

		return $host;
		
	} // end get_mail_host
*/	
	
	static function get_os_line_ending()
	{
		if (strtoupper(substr(PHP_OS,0,3)=='WIN'))
		{
		  $eol="\r\n";
		}
		elseif (strtoupper(substr(PHP_OS,0,3)=='MAC'))
		{
		  $eol="\r";
		}
		else
		{
		  $eol="\n";
		}

		return $eol;
	}
}

?>